<?php 
/**
 *  Por Federico Pisarello - SisDev Software (c) 2012 - Buenos Aires, Argentina.
 * 	fpisarello@sisdevsoft.com
 */

require("includes/funciones.inc.php");

session_start();
if(!isset($_SESSION['idUser']))
	exit();

function searchABMHorses()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");
	require("includes/datatables_db/Datatables.php");
	
	$datatables = new Datatables();
	
	$datatables
			->select('h.idHorse, h.name, h.hair, hr.dateRacing, u.userCompleteName')
			->from('horses h')
			->join('users u','h.idUser = u.idUser')
			->join('(SELECT idHorse, MAX(dateRacing) AS dateRacing FROM horses_racing GROUP BY idHorse) hr', 'h.idHorse = hr.idHorse', 'left')
			->edit_column('hr.dateRacing', '$1', 'formatoFecha(hr.dateRacing)')
			->where("state <> '".$stateDeleted."'");
			
	if($_SESSION['idTypeUser'] != $idTypeAdministrator)
		$datatables->where("h.idUser = ".$_SESSION['idUser']);

	return $datatables->generate();	
}

function searchRacing()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");
	require("includes/datatables_db/Datatables.php");
	
	$idHorse = $_REQUEST["idHorse"];
	
	$datatables = new Datatables();
	
	$datatables
			->select('idHorseRacing, dateRacing, distance')
			->from('horses_racing')
			->edit_column('dateRacing', '$1', 'formatoFecha(dateRacing)')
			->edit_column('idHorseRacing', '<img style="cursor:pointer;" onclick="editItemRacingProxyShow($1)" src="images/editar.png"/>&nbsp;<img style="cursor:pointer;" onclick="deleteItemRacing($1)" src="images/eliminar.png"/>', 'idHorseRacing')
			->where("idHorse = ".$idHorse);

	return $datatables->generate();
}

function addOrEdit()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");

	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;

	$DB->StartTrans(); // start transaction
	
	if($_SESSION['idTypeUser'] == $idTypeAdministrator)
		$idUser = $_REQUEST["horseUser"];
	else
		$idUser = $_SESSION['idUser'];
		
	if(!isset($_REQUEST["idHorse"])){
			
		$strSQL = "INSERT INTO horses ( ";
		$strSQL .= "	name, ";
		$strSQL .= "	yearsOld, ";
		$strSQL .= "	hair, ";
		$strSQL .= "	pretreatments, ";
		$strSQL .= "	temperament, ";
		$strSQL .= "	state, ";
		$strSQL .= "	idUser ";
		$strSQL .= "	) VALUES ( ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["horseName"], "UTF-8")."', ";
		$strSQL .= "	'".$_REQUEST["horseYears"]."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["horseHair"], "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["horsePretreatments"], "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["horseTemperament"], "UTF-8")."', ";
		$strSQL .= "	'".$stateActive."', ";
		$strSQL .= "	".$idUser;
		$strSQL .= "	)";
		
		$DB->Execute($strSQL);
	}
	else{
		$strSQL = "UPDATE horses SET ";
		$strSQL .= "	name = '".mb_strtoupper($_REQUEST["horseName"], "UTF-8")."', ";
		$strSQL .= "	yearsOld = '".$_REQUEST["horseYears"]."', ";
		$strSQL .= "	hair = '".mb_strtoupper($_REQUEST["horseHair"], "UTF-8")."', ";
		$strSQL .= "	pretreatments = '".mb_strtoupper($_REQUEST["horsePretreatments"], "UTF-8")."', ";
		$strSQL .= "	temperament = '".mb_strtoupper($_REQUEST["horseTemperament"], "UTF-8")."', ";
		$strSQL .= "	state = '".$stateActive."',";
		$strSQL .= "	idUser = ".$idUser;
		$strSQL .= " WHERE idHorse = ". $_REQUEST["idHorse"];
		
		$DB->Execute($strSQL);
	}
	
	if (!$DB->CompleteTrans())
	{
		$arrJSON->status = "ERROR";
		$arrJSON->msg = $a_languages['Common.ErrorGrabar'];
	}
	else
	{
		$arrJSON->status = "OK";
		$arrJSON->msg = $a_languages['Common.OkGrabar'];
	}	
	return json_encode($arrJSON);
}

function addOrEditItem()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");
	
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$DB->StartTrans(); // start transaction
	
	$dateRace = date("Y-m-d", strtotime(str_replace("/", "-", $_REQUEST['horseDateRace'])));
	$idHorse = $_REQUEST["idHorse"];
	
	if(!isset($_REQUEST["idHorseRacing"])){
			
		$strSQL = "INSERT INTO horses_racing ( ";
		$strSQL .= "	idHorse, ";
		$strSQL .= "	dateRacing, ";
		$strSQL .= "	distance ";
		$strSQL .= "	) VALUES ( ";
		$strSQL .= "	'".$idHorse."', ";
		$strSQL .= "	'".$dateRace."', ";
		$strSQL .= "	'".$_REQUEST["horseRaceDistance"]."' ";
		$strSQL .= "	)";
	
		$DB->Execute($strSQL);
	}
	else{
		$strSQL = "UPDATE horses_racing SET ";
		$strSQL .= "	dateRacing = '".$dateRace."', ";
		$strSQL .= "	distance = '".$_REQUEST["horseRaceDistance"]."' ";
		$strSQL .= " WHERE idHorseRacing = ". $_REQUEST["idHorseRacing"];
	
		$DB->Execute($strSQL);
	}
	
	if (!$DB->CompleteTrans())
	{
		$arrJSON->status = "ERROR";
		$arrJSON->msg = $a_languages['Common.ErrorGrabar'];
	}
	else
		$arrJSON->status = "OK";
	
	return json_encode($arrJSON);
}

function del()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");
	
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$DB->StartTrans(); // start transaction
	$DB->Execute("UPDATE horses SET state='$stateDeleted' WHERE idHorse =" . $_REQUEST["idHorse"]); // execute query

	if (!$DB->CompleteTrans())
	{
		$arrJSON->status = "ERROR";
		$arrJSON->msg = $a_languages['Common.ErrorEliminar'];
	}
	else
	{
		$arrJSON->status = "OK";
		$arrJSON->msg = $a_languages['Common.OkEliminar'];
	}	
	return json_encode($arrJSON);
}

function delItem()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");

	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;

	$DB->StartTrans(); // start transaction
	$DB->Execute("DELETE FROM horses_racing WHERE idHorseRacing =" . $_REQUEST["idHorseRacing"]); // execute query

	if (!$DB->CompleteTrans())
	{
		$arrJSON->status = "ERROR";
		$arrJSON->msg = $a_languages['Common.ErrorEliminar'];
	}
	else
		$arrJSON->status = "OK";

	return json_encode($arrJSON);
}

function getUsers()
{
	require("includes/constants.php");

	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$rsUsers = $DB->Execute("SELECT * FROM users WHERE userState <> '$stateDeleted' ORDER BY userCompleteName");
	
	$i=0;
	while(!$rsUsers->EOF){
		$arrUsers->data[$i]['idUser'] = $rsUsers->fields('idUser');
		$arrUsers->data[$i++]['userCompleteName'] = $rsUsers->fields('userCompleteName');
		$rsUsers->MoveNext();
	}
	
	return json_encode($arrUsers->data);
}

function editHorse()
{
	require("includes/constants.php");
	
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$rsHorse = $DB->Execute("SELECT * FROM horses WHERE state <> '$stateDeleted' AND idHorse = " . $_REQUEST["idHorse"]); // execute query
						
	if(!$rsHorse->EOF){
		$joHorse->data['horseName'] = $rsHorse->fields("name");
		$joHorse->data['horseYears'] = $rsHorse->fields("yearsOld");
		$joHorse->data['horseHair'] = $rsHorse->fields("hair");
		$joHorse->data['horsePretreatments'] = $rsHorse->fields("pretreatments");
		$joHorse->data['horseTemperament'] = $rsHorse->fields("temperament");
		$joHorse->data['idUser'] = $rsHorse->fields("idUser");
		
		return json_encode($joHorse->data);
	}
	else
		return json_encode('');
}

function editRacing()
{
	require("includes/constants.php");

	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;

	$rsHorse = $DB->Execute("SELECT * FROM horses_racing WHERE idHorseRacing = " . $_REQUEST["idHorseRacing"]); // execute query

	if(!$rsHorse->EOF){
		$joHorse->data['horseDateRacing'] = date("d/m/Y", strtotime($rsHorse->fields("dateRacing")));
		$joHorse->data['horseDistance'] = $rsHorse->fields("distance");

		return json_encode($joHorse->data);
	}
	else
		return json_encode('');
}

switch($_REQUEST['actionOfForm'])
{
	case "EDIT":
		echo editHorse();
		break;
	case "RACING":
		echo editRacing();
		break;
	case "DELETE":
		echo del();
		break;
	case "DELETEITEM":
		echo delItem();
		break;
	case "search":
		echo searchABMHorses();
		break;
	case "addOrEdit":
		echo addOrEdit();
		break;
	case "addOrEditItem":
		echo addOrEditItem();
		break;
	case "searchRacing":
		echo searchRacing();
		break;
	case "getUsers":
		echo getUsers();
		break;
}
?>
